/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CoursesPanel.java
 *
 * Created on 11/03/2009, 20:08:11
 */
package br.unisc.pdaapp.ui;

import br.unisc.pdaapp.constants.IPanels;
import br.unisc.pdaapp.meta.Course;
import br.unisc.pdaapp.meta.ILoadableForm;
import br.unisc.pdaapp.meta.User;
import br.unisc.pdaapp.webservice.client.WSHandler;
import br.unisc.pdaapp.xml.MoodleXmlReader;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JLabel;
import sun.misc.BASE64Decoder;

/**
 *
 * @author guilherme
 */
public class CoursesPanel extends javax.swing.JPanel implements ILoadableForm
{

    private static CoursesPanel instance;

    /** Creates new form CoursesPanel */
    private CoursesPanel()
    {
        initComponents();
    }

    public static CoursesPanel getInstance()
    {
        if (instance == null)
        {
            instance = new CoursesPanel();
        }

        return instance;
    }

    /**
     * Carrega os cursos.
     */
    private void loadCourses()
    {
        coursesPanel.removeAll();

        WSHandler wsHandler;
        try
        {
            wsHandler = new WSHandler();

            User user = MoodleMobileFrame.getInstance().getUser();
            byte[] result = wsHandler.getCourses(user.getUserName());

            BASE64Decoder based = new BASE64Decoder();

            String encoded = new String(result);
            byte[] decoded = based.decodeBuffer(encoded);
            String xml = new String(decoded);
            System.out.println("xml: " + xml);

            MoodleXmlReader moodleXmlReader = new MoodleXmlReader();
            moodleXmlReader.readXML(xml, MoodleXmlReader.COURSE, decoded);
            Vector courses = moodleXmlReader.getResultData();

            GridBagConstraints labelGridBagConstraints = null;
            GridBagConstraints buttonGridBagConstraints = null;
            JLabel courseLabel = null;
            JButton buttonGo = null;
            
            for (int x = 0; x < courses.size(); x++)
            {
                final Course course = (Course) courses.get(x);

                labelGridBagConstraints = new GridBagConstraints();
                labelGridBagConstraints.weightx = 1;
                labelGridBagConstraints.weighty = ((x == courses.size()-1) ? 1 : 0);
                labelGridBagConstraints.gridx = 1;
                labelGridBagConstraints.gridy = x;
                labelGridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
                labelGridBagConstraints.insets = new Insets(4, 2, 0, 2);

                buttonGridBagConstraints = new GridBagConstraints();
                buttonGridBagConstraints.weightx = 0;
                buttonGridBagConstraints.weighty = ((x == courses.size()-1) ? 1 : 0);
                buttonGridBagConstraints.gridx = 0;
                buttonGridBagConstraints.gridy = x;
                buttonGridBagConstraints.anchor = GridBagConstraints.NORTHEAST;
                buttonGridBagConstraints.insets = new Insets(2, 2, 0, 2);

                courseLabel = new JLabel(course.getName());
                courseLabel.setFont(new java.awt.Font("Tahoma", 0, 10));
                buttonGo = new JButton();
                buttonGo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unisc/pdaapp/ui/images/forward.jpeg")));
                buttonGo.setContentAreaFilled(false);
                buttonGo.setFont(new java.awt.Font("Tahoma", 0, 10));
                buttonGo.setBorder(null);

                buttonGo.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e)
                    {
                        int courseID = Integer.parseInt(course.getId());
                        CourseClasseslPanel.getInstance().loadClassesBy(courseID);
                        MoodleMobileFrame.getInstance().switchPanel(IPanels.CLASSES_PANE);
                        CourseClasseslPanel.getInstance().setHeader(course.getName());
                    }
                });

                coursesPanel.add(courseLabel, labelGridBagConstraints);
                coursesPanel.add(buttonGo, buttonGridBagConstraints);
            }

        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }

    public void loadForm()
    {
        loadCourses();
    }

    public void setHeader(String text)
    {
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel1 = new javax.swing.JPanel();
        backButton1 = new javax.swing.JButton();
        exitButton = new javax.swing.JButton();
        mainPanel = new javax.swing.JPanel();
        northPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        coursesScrollPane = new javax.swing.JScrollPane();
        coursesPanel = new javax.swing.JPanel();

        setBackground(new java.awt.Color(255, 255, 255));
        setLayout(new java.awt.BorderLayout());

        jPanel1.setOpaque(false);
        jPanel1.setLayout(new java.awt.GridBagLayout());

        backButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unisc/pdaapp/ui/images/back.jpeg"))); // NOI18N
        backButton1.setBorder(null);
        backButton1.setOpaque(false);
        backButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backButton1ActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 1);
        jPanel1.add(backButton1, gridBagConstraints);

        exitButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unisc/pdaapp/ui/images/close.jpeg"))); // NOI18N
        exitButton.setBorder(null);
        exitButton.setOpaque(false);
        exitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel1.add(exitButton, gridBagConstraints);

        add(jPanel1, java.awt.BorderLayout.NORTH);

        mainPanel.setBackground(new java.awt.Color(255, 255, 255));
        mainPanel.setOpaque(false);
        mainPanel.setLayout(new java.awt.BorderLayout());

        northPanel.setOpaque(false);
        northPanel.setLayout(new java.awt.GridBagLayout());

        jLabel1.setText("Disciplinas:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(3, 5, 5, 5);
        northPanel.add(jLabel1, gridBagConstraints);

        mainPanel.add(northPanel, java.awt.BorderLayout.NORTH);

        coursesScrollPane.setBackground(new java.awt.Color(255, 255, 255));
        coursesScrollPane.setOpaque(false);

        coursesPanel.setBackground(new java.awt.Color(255, 255, 255));
        coursesPanel.setLayout(new java.awt.GridBagLayout());
        coursesScrollPane.setViewportView(coursesPanel);

        mainPanel.add(coursesScrollPane, java.awt.BorderLayout.CENTER);

        add(mainPanel, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void backButton1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_backButton1ActionPerformed
    {//GEN-HEADEREND:event_backButton1ActionPerformed
        MoodleMobileFrame.getInstance().switchPanel(IPanels.LOGIN_PANE);
}//GEN-LAST:event_backButton1ActionPerformed

    private void exitButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_exitButtonActionPerformed
    {//GEN-HEADEREND:event_exitButtonActionPerformed
        MoodleMobileFrame.getInstance().quit(this);
}//GEN-LAST:event_exitButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton backButton1;
    private javax.swing.JPanel coursesPanel;
    private javax.swing.JScrollPane coursesScrollPane;
    private javax.swing.JButton exitButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JPanel northPanel;
    // End of variables declaration//GEN-END:variables

}
